﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace ChroniclesOfVivandia.UI.TileEngine
{
    /// <summary>
    /// Represents a zone in a tileset, zones are always composed of 2x3 tiles that can align to create large zones of a specific complex tile
    /// </summary>
    class TilesetTextureZone
    {

        public enum ZoneTileType
        {
            Single,
            Cross,
            OuterTopLeft,
            OuterTopRight,
            OuterBottomLeft,
            OuterBottomRight,
            OuterTop,
            OuterLeft,
            OuterRight,
            OuterBottom,
            Inner
        }
        private Point _ZoneLocation;

        /// <summary>
        /// Builds a tileset zone from a location in a tileset and provides easy mechanism to draw zones with it
        /// </summary>
        /// <param name="ZoneLocation">Location where this zone starts</param>
        public TilesetTextureZone(Point ZoneLocation)
        {
            this._ZoneLocation = ZoneLocation;
        }

        /// <summary>
        /// Returns the tile to draw based on the zone tile you want to get
        /// </summary>
        public Point ZoneTile(ZoneTileType tileType)
        {
            switch (tileType)
            {
                case ZoneTileType.Cross:
                    return new Point(_ZoneLocation.X + 1, _ZoneLocation.Y);
                case ZoneTileType.OuterTopLeft:
                    return new Point(_ZoneLocation.X, _ZoneLocation.Y + 1);
                case ZoneTileType.OuterTopRight:
                    return new Point(_ZoneLocation.X + 1, _ZoneLocation.Y + 1);
                case ZoneTileType.OuterBottomLeft:
                    return new Point(_ZoneLocation.X, _ZoneLocation.Y + 2);
                case ZoneTileType.OuterBottomRight:
                    return new Point(_ZoneLocation.X + 1, _ZoneLocation.Y + 2);
                case ZoneTileType.OuterTop:
                    
                case ZoneTileType.Single:
                default:
                    return _ZoneLocation;
            }
        }

        public List<DrawnTile> GenerateDrawnTiles(TilesetTexture Tileset, Vector2 From, Vector2 Size)
        {
            //Setup the results array
            List<DrawnTile> results = new List<DrawnTile>();

            //Generate the tiles
            for (int y = 0; y < Size.Y; y++)
            {
                for (int x = 0; x < Size.X; x++)
                {
                    //Setup the target position
                    Vector2 Target = new Vector2(From.X + Tileset.TileSize.X * x, From.Y + Tileset.TileSize.Y * y);

                    //If y and x equals one of the corners, return a corner, else return a full
                    if (y == 0 && x == 0)
                    {
                        results.Add(new DrawnTile(Tileset, Target, this.ZoneTile(ZoneTileType.OuterTopLeft)));
                    }
                    else if (y == 0 && x == Size.X - 1)
                    {
                        results.Add(new DrawnTile(Tileset, Target, this.ZoneTile(ZoneTileType.OuterTopRight)));
                    }
                    else if (y == Size.Y - 1 && x == 0)
                    {
                        results.Add(new DrawnTile(Tileset, Target, this.ZoneTile(ZoneTileType.OuterBottomLeft)));
                    }
                    else if (y == Size.Y - 1 && x == Size.X - 1)
                    {
                        results.Add(new DrawnTile(Tileset, Target, this.ZoneTile(ZoneTileType.OuterBottomRight)));
                    }
                    else if (y == 0 && x != Size.X - 1 && x != 0)
                    {
                        results.Add(new DrawnTile(Tileset, Target, this.ZoneTile(ZoneTileType.OuterTop)));
                    }
                    else if (y == Size.Y - 1 && x != Size.X - 1 && x != 0)
                    {
                        results.Add(new DrawnTile(Tileset, Target, this.ZoneTile(ZoneTileType.OuterBottom)));
                    }
                    else if (y != Size.Y - 1 && y != 0 && x == 0)
                    {
                        results.Add(new DrawnTile(Tileset, Target, this.ZoneTile(ZoneTileType.OuterLeft)));
                    }
                    else if (y != Size.Y - 1 && y != 0 && x == Size.X - 1)
                    {
                        results.Add(new DrawnTile(Tileset, Target, this.ZoneTile(ZoneTileType.OuterRight)));
                    }
                    else
                    {
                        results.Add(new DrawnTile(Tileset, Target, this.ZoneTile(ZoneTileType.Inner)));
                    }
                }
            }

            //Return the results
            return results;

        }

    }
}
